package com.outstudio.plan.garbage;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;

import com.itextpdf.text.BadElementException;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfWriter;

public class TransPngToPdf {

	public static void trans(String outPdfPath, String imgPath) {
		trans(outPdfPath, imgPath, PageSize.A4);
	}

	public static void trans(String outPdfPath, String imgPath, Rectangle pageSize) {
		try {
			Document document = new Document(pageSize);
			PdfWriter pw = PdfWriter.getInstance(document, new FileOutputStream(outPdfPath));
			document.open();

			Image image = Image.getInstance(imgPath);
			image.scaleToFit(pageSize);
			document.add(image);

			document.close();
			pw.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (BadElementException e) {
			e.printStackTrace();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (DocumentException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		trans("E://svg/pdf20160124.pdf", "E:/svg/png20160124.png");
	}

}
